<!--
+----------------------------------------------------------------------
| 友得云客  - 开启房产营销新纪元
+----------------------------------------------------------------------
| Copyright (c) 2019~2023 优得（西安）信息科技有限公司版权所有
+----------------------------------------------------------------------
| Licensed 友得云客不是自由软件 未经允许不可移除相关版权
+----------------------------------------------------------------------
| Author: www.youdeyunke.com
+----------------------------------------------------------------------
-->
<template>
    <div class="meta-content-manager" v-cloak v-loading="loading"  :element-loading-text="loadingText" >
        <ai-spider @success="loadData" @close="loadData"  spiderId="POST_META"  ref="aiSpider" ></ai-spider>
        <div class="btns">
            <div v-if="showAiSpider" style="margin: 0 10px;">
                <el-button :disabled="loading" size="small" title="AI智能填写" :loading="loading"  loadingText="正在填写，请等待..." @click="spiderHandle">
                    <i class="ai-icon"></i>AI智能填写</el-button>
            </div>
            <el-button :disabled="loading" size="small" @click="submitHandle" icon="el-icon-check" type="primary" >保存</el-button>
        </div>
        <meta-editor ref="metaEditor" :rows="15" v-model="metaData.value"></meta-editor>
        <el-alert style="margin-top: 20px" type="info" :closable="false">厂房参数格式说明：以冒号分割，按回车换行，例如"车位数：200个"</el-alert>

    </div>
</template>

<script>
import { getMetaContent, updateMetaContent, } from "@/api/meta-content";
import AiSpider from "@/components/AiSpider/index.vue";
import MetaEditor from "@/components/MetaEditor";

export default {
    components: {
        MetaEditor,
        AiSpider,
    },
    data() {
        return {
            metaData: { id: null, value: "" },
            loadingText: "数据加载中...",
            loading: true,
        };
    },
    props: {
        metaContentId: {
            type: Number,
        },
        postId: {
            type: Number,
        },
        showAiSpider: {
            type: Boolean,
            default: false,
        },
    },
    watch: {
        metaContentId: {
            immediate: true,
            deep: true,
            handler(val) {
                if (!val) {
                    return false;
                }
                this.loadData();
            },
        },
    },

    mounted: function () { },

    computed: {},

    methods: {
        spiderSuccess: function(res){
            console.log('spider success', res);
            this.loadData();
        },


        spiderHandle() {
            if(!this.postId){
                this.$message.error("参数错误：post_id");
                return;
            }
            if(this.metaData.value && this.metaData.value.length > 0){
                // 覆盖提示
                this.$confirm("自动填写会覆盖当前厂房参数，是否继续？").then(() => {
                    console.log('loading');
                    this.spiderRun();
                    return;
                })
            }else{
                this.spiderRun();
            }

        },
        spiderRun(){
            var data = {post_id: this.postId};
            this.$refs.aiSpider.start(data);
        },
        submitHandle: function () {
            if (this.metaData.value.length == 0) {
                this.$message.warning("参数不能为空");
                return false;
            }
            this.loading = true;
            updateMetaContent(this.metaData).then((resp) => {
                this.loading = false;
                if (resp.status == 0) {
                    this.loadData();
                    this.$message.success("已保存");
                }
            }).catch((err) => {
                this.loading = false
                console.log(err);
            });;
        },
        loadData: function () {
            this.loading = true;
            getMetaContent(this.metaContentId).then((resp) => {
                this.loading = false;
                if (resp.status == 0) {
                    this.metaData = resp.data;
                }
            });
        },
    },
};
</script>

<style scoped>
.btns {
    margin-bottom: 20px;
    display: flex;
    justify-content: flex-end;
}

</style>
